package com.glory.dgcpcpda.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import java.util.List;


public class PackageCheckAdapter extends BaseQuickAdapter<LotInfoEntity, BaseViewHolder> {

   private onEditListener listener;

   public interface onEditListener {
      void onEditListener(String materielID);
   }

   public void setListener(onEditListener listener) {
      this.listener = listener;
   }

   public PackageCheckAdapter(@Nullable List<LotInfoEntity> data) {
      super(R.layout.item_package,data);
   }

   @Override
   protected void convert(BaseViewHolder helper, LotInfoEntity item) {
      helper.setIsRecyclable(false);
      helper.setText(R.id.tvMaterialName,item.getMATERIALNAME());
      helper.setText(R.id.tvDescRiptionspec,item.getDESCRIPTIONSPEC());
      EditText etDefectNum = helper.getView(R.id.etMaterielID);
      setEditListener(etDefectNum,item);
   }

   private void setEditListener(EditText editText, LotInfoEntity item) {
      if (editText.getTag() instanceof TextWatcher) {
         editText.removeTextChangedListener((TextWatcher) editText.getTag());
      }

      TextWatcher watcher = new TextWatcher() {
         @Override
         public void beforeTextChanged(CharSequence s, int start, int count, int after) {

         }

         @Override
         public void onTextChanged(CharSequence s, int start, int before, int count) {

         }

         @Override
         public void afterTextChanged(Editable s) {
            try {
               item.setVALUE(s.toString());
               if (s.toString().length() != 0){
                  if(s.toString().length()<=7){
                     editText.setText("");
                  }
                  listener.onEditListener(s.toString());
               }
            } catch (NumberFormatException e) {
               e.printStackTrace();
            }
         }
      };

      editText.addTextChangedListener(watcher);
      editText.setTag(watcher);
   }
}
